package model;


import java.io.File;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.AbstractTableModel;



public class SearchTableModel extends AbstractTableModel{	
	
	private static final long serialVersionUID = 1L;
	
	public SearchTableModel() {
		
	}
	
	public void Clear() {	
		Runnable r = new Runnable() {
			@Override
			public void run() {				
				data.clear();
			}			
		};		
		r.run();		
	}
	
	public void Add(File file) {		
		FileSystemView view = FileSystemView.getFileSystemView();
		Icon img = view.getSystemIcon(file.getAbsoluteFile());		    
		JLabel fileL = new JLabel(file.getAbsolutePath());						  			
		fileL.setIcon(img);						
		
		ArrayList<Object> objects = new ArrayList<Object>();
		objects.add(fileL);		
		objects.add(file);				
		data.add(objects);				
	}	
	
	public void UpdateNames() {
		columnNames.clear();		
		columnNames.add(GlobalParams.getInstance().stringCollection.getNameFileMenu());
				
	}	
	
	
	@Override
	public int getColumnCount() {
		return columnNames.size();
	}
	
	@Override
	public String getColumnName(int arg0) {		
		return columnNames.get(arg0);
	}
	
	@Override
	public int getRowCount() {		
		return data.size();
	}
	
	@Override
	public Object getValueAt(int arg0, int arg1) {		
		return data.get(arg0).get(arg1);
	}
	
	@SuppressWarnings({"unchecked", "rawtypes" })
	@Override	 
     public Class getColumnClass(int columnIndex) {	 
         if(columnIndex == 0)           	 
        	 return getValueAt(0, columnIndex).getClass(); 	 
         
         else 
        	 return super.getColumnClass(columnIndex);
   
     }
	
	public File getSelectedRow(int i) {
		File file;
		try {
			file = (File) data.get(i).get(1);	
		} catch (Exception e) {
			file = null;			
		}		
		return file;
	} 
	
	
	private ArrayList <String> columnNames = new ArrayList <String>();	
	private ArrayList <ArrayList <Object > > data  = new ArrayList <ArrayList <Object > >();
}
